#include "login.h"
#include "./Forms/ui_login.h"

#include <QPushButton>
#include <QCheckBox>
#include <QLineEdit>
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QPixmap>
#include <QIcon>
#include <QFile>
#include <QMessageBox>


login::login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    this->initUI();
    this->setFixedSize(560,400);
    this->setWindowTitle("Login");
    this->setWindowIcon(QIcon(":/icons/yoimiya.jpg"));

    QFile login_style(":/styles/login_gray.css");
    login_style.open(QFile::ReadOnly);
    if (!login_style.isOpen()) {
        QMessageBox::warning(this, "","open failed!");
    }
    this->setStyleSheet(login_style.readAll());
}

login::~login()
{
    delete ui;
}

void login::initUI()
{
    //控件创建和属性初始化
    auto miniSizeBtn = new QPushButton;
    QPixmap pixbtn_m("://icons/minsize_violet.png");
    miniSizeBtn->setIcon(QIcon(pixbtn_m));
    miniSizeBtn->setFixedSize(45, 45);

    auto closeBtn = new QPushButton;
    QPixmap pixbtn_c("://icons/close_small.png");
    closeBtn->setIcon(QIcon(pixbtn_c));
    closeBtn->setFixedSize(45, 45);

    auto logoLab = new QLabel;
    QPixmap pixlab_logo("://icons/yoimiya.jpg");
    pixlab_logo.scaled(logoLab->size());
    logoLab->setPixmap(pixlab_logo);
    logoLab->setFixedSize(120, 100);
    logoLab->setScaledContents(true);

    auto accountEdit = new QLineEdit;
    accountEdit->setFixedSize(350, 40);
    accountEdit->setPlaceholderText("User name");

    auto passwordEdit = new QLineEdit;
    passwordEdit->setFixedSize(350, 40);
    passwordEdit->setPlaceholderText("Password");
    passwordEdit->setEchoMode(QLineEdit::Password);

    auto rememberCbx = new QCheckBox("remember password");
    auto forgetBtn = new QPushButton("forget password?");

    auto loginBtn = new QPushButton("login");
    loginBtn->setFixedSize(350, 35);

    auto messageLab = new QLabel("no acount?");
    auto registerBtn = new QPushButton("create a acount");

    //布局开始
    QGridLayout* mainLayout = new QGridLayout(this);
    mainLayout->addWidget(logoLab,      0, 0, 1, 2);
    mainLayout->addWidget(miniSizeBtn,  0, 5, 1, 1);
    mainLayout->addWidget(closeBtn,     0, 6, 1, 1);
    mainLayout->addWidget(accountEdit,  1, 1, 1, 5);
    mainLayout->addWidget(passwordEdit, 2, 1, 1, 5);
    mainLayout->addWidget(rememberCbx,  3, 1, 1, 1);

    mainLayout->addWidget(forgetBtn,    3, 3, 1, 1);
    mainLayout->addWidget(loginBtn,     4, 1, 1, 5);
    mainLayout->addWidget(messageLab,   5, 2, 1, 1);
    mainLayout->addWidget(registerBtn,  5, 3, 1, 1);

    //布局属性调整
    mainLayout->setHorizontalSpacing(0);
    mainLayout->setVerticalSpacing(0);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    mainLayout->setAlignment(miniSizeBtn, Qt::AlignTop);
    mainLayout->setAlignment(closeBtn, Qt::AlignTop);


}
